package com.qk.server.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.Dto.ClueDto;
import com.qk.common.PageResult;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.server.BusinessesServer;
import com.qk.server.ClueServer;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServerImpl extends ServiceImpl<ClueMapper, Clue> implements ClueServer{
    @Autowired
    ClueMapper clueMapper;
    @Autowired
    ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    BusinessesServer businessesServer;
    @Override
    public PageResult<Clue> findClueByList(ClueDto clueDto) {
        Page<Clue> cluePageResult = clueMapper.selectClueByList(new Page<>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(cluePageResult.getTotal(), cluePageResult.getRecords()) ;
    }
    @Override
    public Clue getClueById(Integer id) {

        return clueMapper.selectClueById(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateClueTrackById(Clue clue) {
        clue.setStatus(3);//跟进中
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatefalseClueTrackById(Integer id, Integer reason, String remark) {
        //查找线索
        Clue clue = clueMapper.selectById(id);
        //clue表修改伪线索
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //线索跟踪表
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setType(0);
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setRecord(remark);
        clueTrackRecord.setFalseReason(reason);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }
    //转为商机
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toBusiness(Integer id) {
        //1.根据id查询线索
        Clue clue = clueMapper.selectById(id);
        //2.将线索设置为转商机
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //3.添加商机
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);
        business.setStatus(1);
        business.setUserId(null);
        business.setClueId(id);
        business.setNextTime(null);
        business.setUpdateTime(LocalDateTime.now());
        business.setCreateTime(LocalDateTime.now());
        businessesServer.save(business);
    }

    @Override
    public PageResult<Clue> findCluePoolList(ClueDto clueDto) {
        Page<Clue> page = clueMapper.selectCluePoolList(new Page<>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }
}
